import { useSyncExternalStore } from 'react';

let store = {
  x: 0,
  y: 0
}

function getSnapshot() {
  return store;
}

function subscribe(callback: any) {
  window.addEventListener('click', (e) => {
    store = { x: e.x, y: e.y }
    callback()
  });
  return () => {
    window.removeEventListener('click', callback);
  };
}

export default function usePosition() {
  const store = useSyncExternalStore(subscribe, getSnapshot);
  return store
}